<?php
/*
*后台页面展示
*/
namespace app\admin\controller;
use think\Controller;
use think\Request;
use \think\Db;
use app\admin\lib\Tree;
class Page extends Base{
     //单页列表
     public function index(){
       $db = Db::name('pagelist');
       $list = $db->order('sort asc')->select();
       //
       $this->assign('listname',$list);
       $uid = $this->select_classname();

       $this->assign('classname',$uid);
        return $this->fetch();
     }
     //通用查询分类名称
     public function select_classname(){
       $db = Db::name('page');
       $list  = $db->select();
       foreach ($list as $key => $value) {
          $name[$value['id']] = $value['name'];
       }
       return $name;
     }
     //单页分类列表
     public function classlist(){
        $db = Db::name('page');
        $list = $db->select();
        $lists =  Tree::findmenu($list,'---');
        $this->assign('listclass',$lists);
        return $this->fetch();
     }
     //添加单页分类
     public function addclass(){
       $db = Db::name('page');
        if($_POST['name']){
          $data['name'] = $_POST['name'];
          $data['uid'] = $_POST['uid'];
          $ok = $db ->insert($data);
          if ($ok) {
            $this->success('操作成功');
          }else{
            $this->error('操作失败');
          }
        }else{
            $this->error('请填名称！');
        }
     }
     //异步更改分类名称
     public function upcalss(){
        $db = Db::name('page');
        // dump($_POST);die;

          $data = [
            'name'=>$_POST['name'],
          ];
          $db->where(['id'=>$_POST['id']])->update($data);
          $arr = ['ok' => '操作成功'];
          echo json_encode($arr);

     }
     //删除单页分类
     public function delcalss(){
       $request = Request::instance();//getid
       $getid = $request->route();
       $db = Db::name('page');
       if ($getid['id']) {
         $db->where(['id'=>$getid['id']])->delete();
         $this->success('操作成功！');
       }else{
         $this->error('操作失败！');
       }
     }
     //添加单页
     public function addpage(){
       $db = Db::name('pagelist');
       $request = Request::instance();//getid
       $getid = $request->route();
       if (isset($getid['id'])) {
         $list = $db->where(['id'=>$getid['id']])->find();

         $this->assign('detail',$list);
       }

       //单页分类
       $db = Db::name('page');
       $uid  = $db->select();
        $uidname =  Tree::findmenu($uid,'---');
       $this->assign('classname',$uidname);
        return $this->fetch();
     }
     //添加/修改分页
     public function addpages(){
        $db = Db::name('pagelist');
        if($_POST['name']==''){
            $this->error('请输入单页名称！');
        }
        $data['uid'] = $_POST['uid'];
        $data['sort'] = $_POST['sort'];
        $data['pic'] = $_POST['pic'];
        $data['name'] = $_POST['name'];
        $data['remarks'] = $_POST['remarks'];
        $data['content'] = $_POST['content'];
        if ($_POST['id']) {
          $ok = $db->where(['id'=>$_POST['id']])->update($data);
        }else{
          $data['time'] = time();
          $ok = $db->insert($data);
        }
        if ($ok) {
            $this->success('操作成功',url('Page/index'));
        }else{
            $this->error('操作失败');
        }

     }
     //删除单页
     public function delpage(){
       $db = Db::name('pagelist');
       $request = Request::instance();//getid
       $getid = $request->route();
       if (isset($getid['id'])) {
         $list = $db->where(['id'=>$getid['id']])->delete();
         if ($list) {
           $this->success('操作成功');
         }else{
           $this->error('操作失败');
         }
       }else{
         $this->error('操作失败');
       }

     }
}
